package cn.ym.controller;

import cn.ym.domain.Article;
import cn.ym.dto.ArticleDto;
import cn.ym.exception.BusinessException;
import cn.ym.query.ArticleQuery;
import cn.ym.service.ArticleService;
import cn.ym.utils.AjaxResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
/**
 * 文章的接口
 */
@RestController
@RequestMapping("/article")
@Api(value = "wen的接口的API",description="文章的接口的CRUD功能")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //添加或修改文章信息
    @PutMapping
    @ApiOperation(value = "添加+修改" )
    public AjaxResult save(@RequestBody Article article){
        try {
            if(article.getId()==null){
                articleService.add(article);
            }else {
                articleService.update(article);
            }
            return new AjaxResult();
        }catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试");
        }

    }

    //删除文章信息
    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            articleService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息
            return  new AjaxResult(false,"网络异常，请稍后重试");
        }
    }

    //查询所有文章信息
    @ApiOperation(value = "查询使用文章" )
    @GetMapping
    public List<Article> findAll(){
        return articleService.findAll();
    }

    //查询单个文章信息
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Article findOne(@PathVariable("id") Long id){
        return articleService.findOne(id);
    }


    //分页查询 + 高级查询
    @PostMapping
    @ApiOperation(value = "分页+高级" )
    public PageInfo<Article> queryPage(@RequestBody ArticleQuery articleQuery){
        List<Article> articles = articleService.queryPage(articleQuery);
        return new PageInfo<>(articles);
    }

    //批量删除文章信息
    @ApiOperation(value = "通过ID删除" )
    @PatchMapping
    public AjaxResult Patchdel(@RequestBody Long[]  ids){
        try {
            articleService.Patchdel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息
            return  new AjaxResult(false,"网络异常，请稍后重试");
        }
    }



    //查询所有文章类型
    @ApiOperation(value = "查询所有文章类型" )
    @GetMapping("/statisticsByArticleType")
    public List<ArticleDto> statisticsByArticleType(){
        return articleService.statisticsByArticleType();
    }

    //按照文章发布时间分组统计数量
    @ApiOperation(value = "按照文章发布时间分组统计数量" )
    @GetMapping("/selectArticleTimeStatistics")
    public List<ArticleDto> selectArticleTimeStatistics(){
        return articleService.selectArticleTimeStatistics();
    }

    ////获取相关性文章列表数据
    @ApiOperation(value = "获取相关性文章列表数据" )
    @GetMapping("/getDependentArticles/{id}")
    public List<Article> getDependentArticles(@PathVariable("id") Long id){

        return articleService.getDependentArticles(id);
    }


    //根据文章类型获取所有文章列表信息
    @ApiOperation(value = "根据文章类型  获取所有文章列表信息" )
    @GetMapping("/getArticleListByTypeId/{id}")
    public List<Article> getArticleListByTypeId(@PathVariable("id") Long articleType){

        return articleService.getArticleListByTypeId(articleType);
    }




    //获取归档文章信息
    @ApiOperation(value = "获取归档文章信息" )
    @GetMapping("/getBlogGdList/{time}")
    public List<ArticleDto> getBlogGdList(@PathVariable("time") String time){
        return articleService.getBlogGdList(time);
    }


    /**
     *  获取文章点赞数数据
     */
    @GetMapping("/getArticleStarNum")
    public List<Article> getArticleStarNum(){
        return articleService.getArticleStarNum();
    }
    /**
     *  获取文章分类前5的数据
     */
    @GetMapping("/getArticleType")
    public List<ArticleDto> getArticleType(){
        return articleService.getArticleType();
    }

//    //点赞
//    @ApiOperation(value = "点赞" )
//    @GetMapping("/likeRate/{id}")
//    public List<Article> likeRate(@PathVariable("id") Long id, HttpSession session){
//
//        return articleService.likeRate(id,session);
//    }

}